package ch.epfl.lara.pm.global;

import ch.epfl.lara.pm.ast._

import scala.collection.mutable.HashMap


/**
 * The class table hold the list of declared classes in the source program.
 */
object ClassTable {
  
  val root = "Object"
  
  private val ctable = new HashMap[String,ClassDef] //+((root,ClassDef(false,root,Nil,None,Nil)))
  
  /**
   * <p>
   *    Add a new entry to the class table. Print a error mesage if the entry already exist.
   * </p>
   */
  def addEntry(cld: ClassDef) = ctable.contains(cld.name) match {
    case false => ctable + ((cld.name,cld))
    case true => error("Error: Class "+cld.name+" is already defined in the scope") 
  }
  
   /**
    * <p>
    *    Retrieve the class definition if the name key exists in the table.
    *    Issue an error message if the entry does not exist.
    * </p>
    */
  def lookupClass(name: String): ClassDef = ctable.get(name) match {
    case None =>
        error("Error: Class "+name+" is not defined in the scope")
    case Some(cld) => cld
  }
  
  def containsClass(name: String): Boolean = ctable.contains(name)
    
  /**
   * <p>
   *    Remove all mappings from the class table.
   * </p>
   */
  def clean = ctable.clear
 
  
  def classes = ctable.values.toList
}

